import warnings
warnings.warn("qturboframe.public_ui.ucf_h_ui.button模块已被废弃，替代模块为qturboframe.public_ui.h_ui.button", DeprecationWarning)

from time import sleep

from qturboframe.lib.report.log import logger
from qturboframe.lib.utils.check import CheckBasic
from qturboframe.lib.ranorex.exceptions import CsClientError
from qturboframe.lib.ranorex.web_element.button_tag import ButtonTag
from qturboframe.lib.ranorex.web_element.input_tag import InputTag
from qturboframe.lib.ranorex.web_element.div_tag import DivTag
from qturboframe.lib.ranorex.web_element.li_tag import LiTag
from ..ucf_base_element import BaseElement


class Button(BaseElement, CheckBasic):
    '''hui的按钮操作'''

    def __init__(self, element_para, father=None, timeout=10000):
        super().__init__(element_para, father, timeout)
        element_tag_name = self._element.tag_name
        if element_tag_name == 'button':
            self._element = ButtonTag(self._element.control)
        else:
            self._element = InputTag(self._element.control)

    def click(self):
        '''
        hui的按钮点击

        :Example:
         - 例: button.click()
        '''
        button_text = self._element.text
        if not button_text:
            button_text = self._element.get_attribute('title')
        if button_text:
            logger.info('点击按钮：' + button_text)
        else:
            logger.info('点击按钮！')
        self._element.secure_click()

    def dropdown_click(self, dropdown_text):
        '''
        hui的dropdown按钮点击

        :Args:
         - dropdown_text: dropdown按钮的文本

        :Example:
         - 例: button.dropdown_click('删除')
        '''
        button_text = self._element.text
        if button_text:
            logger.info('点击按钮：' + button_text)
        else:
            logger.info('点击按钮！')
        # 查找按钮组件
        select_father_div = DivTag(self._element.find_element_by_attribute(
            'xpath', './parent::div/parent::div', timeout=self.timeout).control)
        self._element.secure_click()
        timeout = 0
        while timeout < self.timeout:
            # 保持鼠标悬停在按钮上
            self._element.move_to()
            # 查找下拉按钮
            select_li_btn = select_father_div.exist(
                'xpath', './/li[@class~\'h-dropdown-item\' and @innertext=\'{}\']'.format(dropdown_text),
                timeout=100)
            if select_li_btn:
                select_li_btn = LiTag(select_li_btn.control)
                break
            else:
                timeout += 100
                sleep(0.1)
        else:
            raise CsClientError('未找到按钮：{}！'.format(dropdown_text))
        logger.info('点击按钮：' + dropdown_text)
        select_li_btn.secure_click()

    def is_enable(self):
        '''
        判断hui按钮是否可用

        :Returns:
            可用则返回True, 不可用则返回False

        :Example:
         - 例: button.is_enable()
        '''
        if self._element.is_enabled():
            return True
        else:
            return False

    def check_enable(self, state='disable'):
        '''
        hui按钮是否可用校验

        :Attributes:
         - flag: 校验标记，表示需要校验的按钮状态，display表示禁用，enable表示可用

        :Raises:
            不可用则抛出SeleniumError异常

        :Example:
         - 例: button.check_enable('enable')
        '''
        if state == 'disable':
            if self._element.is_enabled():
                raise CsClientError('按钮未禁用！')
        else:
            if not self._element.is_enabled():
                raise CsClientError('按钮已禁用！')
